package com.etime.shycourse.services.wxcourselist;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.etime.shycourse.dao.ChapterMapper;
import com.etime.shycourse.dao.CourseMapper;
import com.etime.shycourse.dao.LessonMapper;
import com.etime.shycourse.dao.OrganizationMapper;
import com.etime.shycourse.dto.WXOffLineCourseDetailDto;
import com.etime.shycourse.dto.WXOffLineCourseShowDto;
import com.etime.shycourse.pojo.Chapter;
import com.etime.shycourse.pojo.Lesson;
import com.etime.shycourse.pojo.Organization;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 微信端-线下课程相关数据接口服务层实现类
 */
@Service
public class WXOffLineCouresServiceImpl implements WXOffLineCouresService {

    @Autowired
    private CourseMapper courseMapper;
    @Autowired
    private ChapterMapper chapterMapper;
    @Autowired
    private LessonMapper lessonMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    /**
     * 查询线下课程详细信息
     *
     * @param courseid 课程id
     * @return 结果包含：课程详情，章节列表，课时列表
     */
    @Override
    public WXOffLineCourseDetailDto selectOffLineCourseDetail(Integer courseid) {
        //查询课程详情
        WXOffLineCourseShowDto course = courseMapper.selectCourseShowById(courseid);
        //查询课程章节列表
        List<Chapter> chapterList = selectChapterListByCourseId(courseid);
        //根据课程id查询课时列表
        List<Lesson> lessonList = lessonMapper.selectLessonListByCourseId(courseid);
        //组和成最终结果
        WXOffLineCourseDetailDto dto = new WXOffLineCourseDetailDto();
        dto.setCourse(course);
        dto.setChapterList(chapterList);
        dto.setLessonList(lessonList);
        //查询课程所属机构信息
        if(course.getShoworginfo()!=null && course.getShoworginfo()==1){
            Organization organization = organizationMapper.selectById(course.getCourseorgid());
            if(organization!=null){
                dto.setOrganization(organization);
            }
        }
        return dto;
    }

    /**
     * 根据课程id查询课程的章节列表
     * @param courseid 课程id
     * @return
     */
    private List<Chapter> selectChapterListByCourseId(Integer courseid){
        QueryWrapper<Chapter> wrapper = new QueryWrapper<>();
        wrapper.eq("courseid",courseid);
        //wrapper.eq("chapterstate",1);
        List<Chapter> chapterList = chapterMapper.selectList(wrapper);
        return chapterList;
    }
}
